#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
This is a test case

"""


import sys, os, subprocess
import argparse

sys.path.append(".")
import super_parser



# main function to test the module

def main(argv=None):
    if argv is None:
      argv = sys.argv
    print argv
    
    reload(sys) 
    sys.setdefaultencoding('utf-8')
    



    # parse the command line arguments and options
    parser = argparse.ArgumentParser()
    parser.add_argument("-time", "--delay_time", \
		      type=float, \
		      default=2, \
		      help="float type the delay time to get the full response from outside ")
    args = parser.parse_args()   

    image_url_list = [r'http://ecx.images-amazon.com/images/I/51XsTd-0xjL._SL135_.jpg',		      
                      r'http://ecx.images-amazon.com/images/I/91-QfaH7kSL._AA1500_.jpg',
	              r'http://ecx.images-amazon.com/images/I/51bzgoxGsjL._AA160_.jpg',
                      r'http://ecx.images-amazon.com/images/I/31DNBOWDTbL._SL500_AA300_.jpg',                      
                      r'http://l4.zassets.com/images/z/2/2/0/6/3/1/2206312-p-MULTIVIEW.jpg',
                      r'http://a2.zassets.com/images/z/2/0/1/6/7/7/2016779-p-MULTIVIEW.jpg',
                      r'http://www.bestbuy.ca/multimedia/Products/500x500/102/10203/10203452.jpg',
                      r'http://www.bestbuy.ca/multimedia/Products/150x150/102/10234/10234642.jpg',                      
                      r'http://lh5.googleusercontent.com/public/7hOFYiRLQ3LPLqMJoAqaRP6GtQfVL6byjaFHgVW-3zEc6KeNGQNRuXKBb5uGqB-rEvteKozHknsykWg54xyyneHnodhht09eznHQMb3T6QJsMrN-Or0ce_iQ-EKfMhz8LLfwq73KlawCDI_CCUolD_8VL2JlH1HTq7eCxfp_eVL8M9g',
                      r'http://lh5.googleusercontent.com/public/-JwhOHQVWLeXc1uE7macmd7pPxC0TBHwBomJp4vLoYAF-YYS59hBuxaCbI7buRgCBM0I846gW9IDvvwBEr9ACnDWd2OnGEWw0AxFbF2Cix8T2sR9gXPOO8FFRVIS63UuOGEZ2m4pUVIo33qaQ9Nz_Yap3Ig=s220-c']
    
    #image_url_list = [r'http://lh5.googleusercontent.com/public/-JwhOHQVWLeXc1uE7macmd7pPxC0TBHwBomJp4vLoYAF-YYS59hBuxaCbI7buRgCBM0I846gW9IDvvwBEr9ACnDWd2OnGEWw0AxFbF2Cix8T2sR9gXPOO8FFRVIS63UuOGEZ2m4pUVIo33qaQ9Nz_Yap3Ig=s220-c']

    
    
    image_url = "empty"
    delay_time   =args.delay_time
    resp_html = "imageSearchResp.html"
    print "Delay time is : " + str(delay_time)
    print "output html is: " + resp_html
    
    test_id = 1
    for image_url in image_url_list:
         print str(test_id)+ "--Image url: " + image_url
         guess_name = "unknown"
         resp_html=str(test_id)+r'.html'
         # call search command and generate response html
         #image_url = r'"'+image_url+r'"'
         #try multiple times to get result
         for iterator in range(1,4):
	    ret = super_parser.get_search_resp_html(url=image_url,out_html=resp_html,wait=delay_time)
	    print "The %d round search ..." %iterator
	    if ret == 0:
		print "search successful, and parsing the name..."
		guess_name = super_parser.extract_match_name_for_html(html_str=None,html_file=resp_html)
		if guess_name is not "unknown":		    
		    break;         
         print "\n!!! Best guess for this image: " + guess_name
         test_id = test_id +1
         print "\n##################################################\n"
	
    

if __name__ == "__main__":
    sys.exit(main())  
